package Client.GUI;

import Client.logic.ClientIF;
import Client.logic.MyFile;
import Server.DataBase.Lesson;
import Server.DataBase.Student;
import Server.Message.MessageGetStudentsInLesson;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageGetTaskId;
import Server.Message.MessageGetTaskIdReplay;
import Server.Message.MessageGradeTask;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageTeacherLesson;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.border.LineBorder;
import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class GradeTaskPanel extends MyJPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldGrade;
	private JComboBox comboBoxStudent, comboBoxLesson, comboBoxTask;
	private Lesson lesson;
	private int taskId;
	private JButton btnChoose, btnSend, btnClear;
	private String filename, dir;
	private Student student;
	private JTextPane textPaneCom;
	
	public GradeTaskPanel(ClientIF client) {
		super(PanelType.GRADE_TASK_PANEL, client);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"grade task.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(0, -6, 699, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setLayout(null);
		init();
	}
	
	public void initLabels(){
		
		JLabel lblLesson = new JLabel("Lesson:");
		lblLesson.setBounds(34, 106, 61, 14);
		add(lblLesson);
		
		JLabel lblTask = new JLabel("Task:");
		lblTask.setBounds(34, 152, 46, 14);
		add(lblTask);
		
		JLabel lblStudent = new JLabel("Student:");
		lblStudent.setBounds(34, 203, 61, 14);
		add(lblStudent);
		
		JLabel lblGrade = new JLabel("Grade:");
		lblGrade.setBounds(325, 235, 46, 14);
		add(lblGrade);
		
		JLabel lblComment = new JLabel("Comment:");
		lblComment.setBounds(292, 106, 79, 14);
		add(lblComment);
		
		JLabel lblFile = new JLabel("File:");
		lblFile.setBounds(34, 249, 46, 14);
		add(lblFile);
	}

	public void initComboBox(){
		comboBoxLesson = new JComboBox();
		comboBoxLesson.setBounds(116, 103, 109, 20);
		add(comboBoxLesson);

		comboBoxTask = new JComboBox();
		comboBoxTask.setBounds(116, 149, 109, 20);
		comboBoxTask.setEnabled(false);
		add(comboBoxTask);
		
		comboBoxStudent = new JComboBox();
		comboBoxStudent.setBounds(116, 200, 109, 20);
		comboBoxStudent.setEnabled(false);
		add(comboBoxStudent);
	}
	
	public void initTextField(){
		textFieldGrade = new JTextField();
		textFieldGrade.setBounds(381, 232, 108, 20);
		add(textFieldGrade);
		textFieldGrade.setColumns(10);

		textPaneCom = new JTextPane();
		textPaneCom.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPaneCom.setBounds(381, 106, 225, 105);
		add(textPaneCom);
	}
	
	public void initBtn(){
		btnChoose = new JButton("Choose");
		btnChoose.setBounds(114, 245, 89, 23);
		btnChoose.setEnabled(false);
		btnChoose.addActionListener(new ChooseFile());
		add(btnChoose);
		
		btnSend = new JButton("Send");
		btnSend.setBounds(198, 300, 89, 23);
		btnSend.setEnabled(false);
		btnSend.addActionListener(new sendFile());
		add(btnSend);
		
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(new GradeTaskPanel(getClient()));
			}
		});
		btnClear.setBounds(311, 300, 89, 23);
		add(btnClear);
	}
	
	public void init(){
		initLabels();
		initBtn();
		initComboBox();
		initTextField();
		getClient().sendMsgToServer(new MessageTeacherLesson(getClient().getUser().getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
		
		comboBoxLesson.addItem("Choose");
		
		
		for (Lesson lesson : msg.getArray())
			comboBoxLesson.addItem(lesson);
		comboBoxLesson.addActionListener(new LessonListener());
	}
	
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				lesson = (Lesson) comboBoxLesson.getSelectedItem();	
				comboBoxLesson.setEnabled(false);
				getClient().sendMsgToServer(new MessageGetTaskId(lesson));
				MessageGetTaskIdReplay msg = (MessageGetTaskIdReplay) getClient().getMessageFromServer();
				comboBoxTask.removeAllItems();
				if (msg.getMaxid()!=0){
					comboBoxTask.addItem("");
					for (int i=1; i<msg.getMaxid(); i++) comboBoxTask.addItem(i);
					comboBoxTask.setEnabled(true);
					comboBoxTask.addActionListener(new TaskListener());
					btnClear.setEnabled(true);
				}
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class TaskListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				taskId =  (Integer) comboBoxTask.getSelectedItem();
				comboBoxTask.setEnabled(false);
				getClient().sendMsgToServer(new MessageGetStudentsInLesson(lesson.getIdLesson()));
				MessageGetStudentsReplay msg2 = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				comboBoxStudent.removeAllItems();
				comboBoxStudent.addItem("Choose");
				for (Student student : msg2.getArray()) 
					comboBoxStudent.addItem(student);
				comboBoxStudent.setEnabled(true);
				comboBoxStudent.addActionListener(new StudentListener());
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class StudentListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
			student =   (Student) comboBoxStudent.getSelectedItem();
			comboBoxStudent.setEnabled(false);
			btnChoose.setEnabled(true);
			} catch (java.lang.Throwable t){}
		}
	}
	
	class ChooseFile implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JFileChooser c = new JFileChooser();
			int rVal = c.showOpenDialog((Component) getClient());
			if (rVal == JFileChooser.APPROVE_OPTION) {
				filename = c.getSelectedFile().getName();
				dir = c.getCurrentDirectory().toString();
				btnSend.setEnabled(true);
			}
			if (rVal == JFileChooser.CANCEL_OPTION) {
				filename = null;
				dir = null;
			}
	    }
	 }
	/** 
	 *teacher send file  add task
	 * */
	 class sendFile implements ActionListener{
		 public void actionPerformed(ActionEvent e) {
			if (checkGrade()){
				String LocalfilePath=dir+File.separator+filename;
				try{
					File newFile = new File (LocalfilePath);  		      
					byte [] mybytearray  = new byte [(int)newFile.length()];
					FileInputStream fis = new FileInputStream(newFile);
					BufferedInputStream bis = new BufferedInputStream(fis);			  
					bis.read(mybytearray,0,mybytearray.length);
					MyFile msg= new MyFile(filename, mybytearray);
					int grade = Integer.parseInt(textFieldGrade.getText());
					getClient().sendMsgToServer(new MessageGradeTask(lesson.getIdLesson(), 
			    		 taskId, student.getIdUser(), grade, textPaneCom.getText(), msg));
					JOptionPane.showMessageDialog((Component) getClient(), "File Send Complete");
					getClient().swapFromBack(new GradeTaskPanel(getClient()));
					}
				catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			else JOptionPane.showMessageDialog((Component) getClient(), "Wrong Grade");
		 }
	 }
	 
	 private boolean checkGrade(){
			try{
				String str = textFieldGrade.getText();
				int g = Integer.parseInt(str);
				if (g>=0 && g<=100) return true;
			} catch (java.lang.Throwable t) {}
			return false;
		}
	
	
	@Override
	public MyJPanel pushPanel() {
		return new GradeTaskPanel(getClient());
	}
	 }

